package com.example.elm.Account

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.Public.AccountInfo
import com.example.elm.R
import com.example.elm.databinding.HongbaoXmlBinding

private val money = ArrayList<String>()
class HongbaoActivity : ComponentActivity() {
    private lateinit var binding: HongbaoXmlBinding
    private lateinit var adapter: MoneyAdapter
    private var AccountInformation: AccountInfo? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = HongbaoXmlBinding.inflate(layoutInflater)
        setContentView(binding.root)
        AccountInformation = CommonUtil.getSerializable(this, "info", AccountInfo::class.java)
        init_money()

        val layoutManager = LinearLayoutManager(this)
        binding.hongbaoUi.layoutManager = layoutManager
        adapter = MoneyAdapter(money)
        binding.hongbaoUi.adapter = adapter

        binding.back.setOnClickListener {
            val intent = Intent()
            intent.putExtra("info", AccountInformation)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private fun init_money() {
        money.clear()
        money.add("10")
        money.add("8")
        money.add("3")
        money.add("7")
        money.add("12")
        money.add("9")
    }

    private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val hongbaoAmount: TextView = view.findViewById(R.id.hongbaoAmount)
    }
    private class MoneyAdapter(val money: ArrayList<String>): RecyclerView.Adapter<ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.hongbao_ui, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val information = money[position]
            holder.hongbaoAmount.text = information
        }

        override fun getItemCount(): Int {
            return money.size
        }
    }
}